package com.example.basicdemo.model.result;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.basicdemo.model.domain.LoginInfoDomain;
import com.example.basicdemo.model.response.OsLoginResponse;
import com.isyscore.os.dev.api.IsyscoreResult;
import com.isyscore.os.dev.util.StringUtils;

import java.util.List;

public class OsLoginResult implements IsyscoreResult<LoginInfoDomain> {

    private OsLoginResponse response;

    public OsLoginResult(OsLoginResponse response) {
        this.response = response;
    }

    public OsLoginResponse getResponse() {
        return response;
    }

    public void setResponse(OsLoginResponse response) {
        this.response = response;
    }

    @Override
    public boolean isSuccess() {
        return response == null ? false : response.isSuccess();
    }

    @Override
    public LoginInfoDomain parseData() {
        if (this.isSuccess()) {
            String body = response.getBody();
            if (!StringUtils.isEmpty(body)) {
                JSONObject bodyJson = JSON.parseObject(body);
                if(bodyJson.get("data") != null){
                    return JSON.parseObject(JSON.toJSONString(bodyJson.get("data")), new TypeReference<LoginInfoDomain>() {
                    });
                }
            }
        }
        return null;
    }

    @Override
    public List<LoginInfoDomain> parseListData() {
        return null;
    }

}
